import { NextRequest, NextResponse } from "next/server";
import prisma from "../../../../lib/prisma";

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ chatId: string }> }
) {
  try {
    const { chatId } = await params;

    const chat = await prisma.chat.findUnique({
      where: { id: chatId },
    });

    if (!chat) {
      return NextResponse.json({ error: "Chat not found" }, { status: 404 });
    }

    return NextResponse.json({ chat });
  } catch (error) {
    return NextResponse.json({ error: "Server error" }, { status: 500 });
  }
}

//删除该聊天
export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ chatId: string }> }
) {
  try {
    const { chatId } = await params;

    const chat = await prisma.chat.delete({
      where: { id: chatId },
    });

    return NextResponse.json({ chat });
  } catch (error) {
    return NextResponse.json({ error: "Server error" }, { status: 500 });
  }
}
